package com.wtgroup.demo.面试题;

/**
 * 数组中有两种数奇数次, 其余偶数次, 找出这两个数.
 * @author dafei
 * @version 0.1
 * @date 2021/4/5 18:05
 */
public class Q005_找奇数个数 {

    public static void main(String[] args) {
        int[] arr = {3, 1, 4, 1, 3, 4, 5, 2, 2, 4, 3, 3}; // 4, 5
        m(arr);
    }

    public static void m(int[] arr) {
        int eor = 0;
        for (int i = 0; i < arr.length; i++) {
            eor ^= arr[i];
        }
        // 000001000
        int rightOne = eor & (-eor);
        int eor1 = 0;
        for (int i = 0; i < arr.length; i++) {
            // 对应为1的才异或
            if ((arr[i] & rightOne) != 0) {
                eor1 ^= arr[i];
            }
        }
        System.out.println(eor1 + " " + (eor^eor1));
    }
}
